package com.droidexchange.activity;

import java.util.List;
import net.sf.stackwrap4j.StackOverflow;
import net.sf.stackwrap4j.entities.Question;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Debug;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.droidexchange.R;
import com.droidexchange.adapter.IPagingAdapter;
import com.droidexchange.adapter.PagingAdapter;
import com.droidexchange.adapter.QuestionsAdapter;
import com.droidexchange.main.DeQuestionsItem;
import com.droidexchange.main.QFilter;
import com.droidexchange.util.Const;
import com.droidexchange.util.Util;

public class QuestionsActivity extends Activity implements IPagingAdapter, OnItemClickListener
{
	private QFilter				qFilter			= null;
	private QuestionsAdapter	qAdapter		= null;
	private PagingAdapter		endlessAdapter	= null;
	private List<Question>		questions		= null;
	private ListView			listView		= null;
	private static final int	MENU_ID_REFRESH	= 0;
	private static final int	MENU_ID_FILTER		= 1;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.question_list);
		listView = (ListView) findViewById(R.id.listview);

		qFilter = QFilter.getQFilter();

		init();

		// setTitle(qFilter.strTitle);

		qAdapter = new QuestionsAdapter(this, R.layout.question_tab);
		endlessAdapter = new PagingAdapter(this, qAdapter);
		listView.setAdapter(endlessAdapter);

		listView.setOnItemClickListener(this);
		qAdapter.clear();

//		Intent intent = new Intent(this, AnswersActivity.class);
//		intent.putExtra("qid", 2986387);//194812
//		startActivity(intent);
		
	}

	private void init()
	{
		Const.init(getApplicationContext());
		qFilter.init(getResources());

		if (Util.sw == null)
			Util.sw = new StackOverflow(Const.APIKEY);
	}

	protected void appendQuestionsToAdapter(QuestionsAdapter qa)
	{
		if (questions == null)
			return;

		Util.log("appendQuestionsToAdapter : questions.size() = " + questions.size());
		for (int i = 0; i < questions.size(); i++)
		{
			qa.add(new DeQuestionsItem(questions.get(i)));
		}
	}

	public Context getContext()
	{
		return this;
	}

	public boolean cacheInBackground()
	{
		Util.log("cacheInBackground()");
		questions = qFilter.getQuestionsList(false);
		if (questions == null)
			return false;

		return (questions.size() != 0);
	}

	public void appendCachedData(ListAdapter listAdapter)
	{
		Util.log("appendCachedData()");
		appendQuestionsToAdapter((QuestionsAdapter) listAdapter);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode != RESULT_OK)
			return;

		try
		{
			switch (requestCode)
			{
				case (Const.INTENT_FILTER_ACTIVITY):
					qAdapter.clear();
				break;
			}
		}
		catch (Exception e)
		{
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int arg2, long arg3)
	{
		String id = (String) view.getTag();
		Intent intent = new Intent(view.getContext(), AnswersActivity.class);
		intent.putExtra("qid", Integer.parseInt(id));
		view.getContext().startActivity(intent);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		menu.add(Menu.NONE, MENU_ID_REFRESH, 0, "Refresh");
		menu.add(Menu.NONE, MENU_ID_FILTER, 1, "Filter");
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
			case MENU_ID_REFRESH:
				qAdapter.clear();
			break;

			case MENU_ID_FILTER:
				Intent intent = new Intent(this, QuestionsFilterActivity.class);
				startActivityForResult(intent, Const.INTENT_FILTER_ACTIVITY);
			break;

			default:
				return false;
		}

		return true;
	}

}
